<?php


namespace coco;


use coco\traits\Singleton;

class Command
{
    use Singleton;

    private $param = [];
    private $controller = '\\app\\console\\Index';
    private $method = '';

    private function __construct()
    {
        global $argv;
        if (empty($argv[1])) {
            return;
        }
        $path = str_replace('/', '\\', $argv[1]);
        $path_array = explode('\\', $path);
        $len = count($path_array);
        //最后个下划线转换驼峰
        $path_array[$len - 1] = url2class($path_array[$len - 1]);
        $controller = '\\app\\console\\' . join('\\', $path_array);
        $method = 'index';
        if (false !== strpos($controller, ':')) {
            list($controller, $method) = explode(':', $controller);
        }
        if (!empty($argv[2]) && substr($argv[2], 0, 1) !== '-') {
            $method = $argv[2];
        }
        $this->controller = $controller;
        $this->method = $method;


        $cur_key = '';
        foreach ($argv as $item) {
            if (substr($item, 0, 1) === '-') {
                $cur_key = substr($item,1);
            } elseif (!empty($cur_key)) {
                $this->param[$cur_key] = $item;
            }
        }
    }


    /**
     * @return string
     */
    public function getController(): string
    {
        return $this->controller;
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * @return array
     */
    public function getParam(): array
    {
        return $this->param;
    }
}